package com.ruoyi.bantong.controller;

import java.util.List;

import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.exception.CustomException;
import com.ruoyi.common.utils.SecurityUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.bantong.domain.BtPlacecardPool;
import com.ruoyi.bantong.service.IBtPlacecardPoolService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 席位池Controller
 * 
 * @author laohei
 * @date 2021-01-24
 */
@RestController
@RequestMapping("/bantong/placecardPool")
public class BtPlacecardPoolController extends BaseController
{
    @Autowired
    private IBtPlacecardPoolService btPlacecardPoolService;

    /**
     * 查询席位池列表
     */
    //@PreAuthorize("@ss.hasPermi('bantong:pool:list')")
    @GetMapping("/list")
    public TableDataInfo list(BtPlacecardPool btPlacecardPool)
    {
        startPage();
        List<BtPlacecardPool> list = btPlacecardPoolService.selectBtPlacecardPoolList(btPlacecardPool);
        return getDataTable(list);
    }

    /**
     * 导出席位池列表
     */
    //@PreAuthorize("@ss.hasPermi('bantong:pool:export')")
    @Log(title = "席位池", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(BtPlacecardPool btPlacecardPool)
    {
        List<BtPlacecardPool> list = btPlacecardPoolService.selectBtPlacecardPoolList(btPlacecardPool);
        ExcelUtil<BtPlacecardPool> util = new ExcelUtil<BtPlacecardPool>(BtPlacecardPool.class);
        return util.exportExcel(list, "pool");
    }

    /**
     * 获取席位池详细信息
     */
    //@PreAuthorize("@ss.hasPermi('bantong:pool:query')")
    @GetMapping(value = "/{ppId}")
    public AjaxResult getInfo(@PathVariable("ppId") Integer ppId)
    {
        return AjaxResult.success(btPlacecardPoolService.selectBtPlacecardPoolById(ppId));
    }

    /**
     * 加入席位池
     */
    //@PreAuthorize("@ss.hasPermi('bantong:pool:add')")
    @Log(title = "席位池", businessType = BusinessType.INSERT)
    @PostMapping("/addPool/{level}")
    public AjaxResult add(@PathVariable("level") Integer level)
    {
        try {
            return AjaxResult.success(btPlacecardPoolService.insertBtPlacecardPool(level));
        } catch (Exception e) {
            e.printStackTrace();
            if (e instanceof CustomException){
                return AjaxResult.error(e.getMessage());
            }else {
                return AjaxResult.error();
            }
        }
    }

    /**
     * 退出席位池
     */
    //@PreAuthorize("@ss.hasPermi('bantong:pool:add')")
    @Log(title = "席位池", businessType = BusinessType.INSERT)
    @PostMapping("/outPool/{level}")
    public AjaxResult out(@PathVariable("level") Integer level)
    {
        try {
            return toAjax(btPlacecardPoolService.outBtPlacecardPool(level));
        } catch (Exception e) {
        e.printStackTrace();
        return AjaxResult.error();
}
    }

    /**
     * 修改席位池
     */
    //@PreAuthorize("@ss.hasPermi('bantong:pool:edit')")
    @Log(title = "席位池", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BtPlacecardPool btPlacecardPool)
    {
        return toAjax(btPlacecardPoolService.updateBtPlacecardPool(btPlacecardPool));
    }

    /**
     * 删除席位池
     */
    //@PreAuthorize("@ss.hasPermi('bantong:pool:remove')")
    @Log(title = "席位池", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ppIds}")
    public AjaxResult remove(@PathVariable Integer[] ppIds)
    {
        return toAjax(btPlacecardPoolService.deleteBtPlacecardPoolByIds(ppIds));
    }

    /**
     * 提前开奖
     */
    @Log(title = "席位池", businessType = BusinessType.UPDATE)
    @PutMapping("/publishTimeNow/{ppId}")
    public AjaxResult updateBtPlacecardPoolPublishTime(@PathVariable Integer ppId)
    {
        return toAjax(btPlacecardPoolService.updateBtPlacecardPoolPublishTime(ppId));
    }

    /**
     * //判断是否 正在参加开奖
     */
    @GetMapping("/ifpool")
    public AjaxResult ifpool()
    {
        try {
            return AjaxResult.success(btPlacecardPoolService.ifPool());
        } catch (Exception e) {
            e.printStackTrace();
            if (e instanceof CustomException){
                return AjaxResult.error(e.getMessage());
            }else {
                return AjaxResult.error();
            }
        }
    }

    /**
     * 获取全部等级 席位池状态
     */
    @GetMapping("/allLevel")
    public TableDataInfo selectBtPlacecardPoolAllLevel() {
        Integer btUserId = SecurityUtils.getBtUserId();
        return getDataTable(btPlacecardPoolService.selectBtPlacecardPoolAllLevel(btUserId));
    }
}
